package com.zzyl.nursing.controller.member;


import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.AddReservationDto;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.ReservationVo;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/member/reservation")
@Api(tags = "会员预约参观接口")
public class MemberReservationController extends BaseController {
    @Autowired
    private IReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("取消的预约数")
    public AjaxResult cancelledCount() {
        Integer count = reservationService.cancelledCount();
        return AjaxResult.success("查询成功",count);
    }

    @GetMapping("/countByTime")
    @ApiOperation("按时间段统计预约数")
    public AjaxResult countByTime(@RequestParam Long time) {
        System.out.println("收到时间戳：" + time);
        return AjaxResult.success(reservationService.countReservationsForTime(time));
    }

    @PostMapping
    @ApiOperation("新增预约")
    public AjaxResult addReservation(
            @Valid @RequestBody AddReservationDto dto
    ) {
        try {
            return reservationService.addReservation(dto)
                    ? AjaxResult.success("预约成功")
                    : AjaxResult.error("预约失败");
        } catch (Exception e) {
            return AjaxResult.error("预约失败：" + e.getMessage());
        }
    }
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public R<TableDataInfo<Reservation>> page(Reservation reservation) {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return R.ok(getDataTable(list));
    }
    @PutMapping("/{id}/cancel")
    @ApiOperation("取消预约")
    public AjaxResult cancel(@PathVariable Long id) {
        reservationService.setstatus(id);
        return AjaxResult.success();
    }



}
